package com.aube.interact.util;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * Created by huyaonan on 16/10/25.
 */
public class Utils {

    public static final String sTAG = Utils.class.getSimpleName();
    private static final boolean DEBUG = true;

    public static enum LogType{
        DEBUG,
        ERROR,
        INFO,
        WARE
    }

    public static void Log(String msg) {
        if (!DEBUG) {
            return;
        }
        Log.i("com.aube", msg);
    }

    public static void Log(String TAG, String msg) {
        if (null == TAG) {
            TAG = sTAG;
        }
        if (!DEBUG) {
            return;
        }
        Log.i(TAG, msg);
    }

    public static void Log(LogType logType,String TAG, String msg) {
        if (null == TAG) {
            TAG = sTAG;
        }
        if (!DEBUG) {
            return;
        }
        if(LogType.DEBUG.compareTo(logType) == 0){
            Log.d(TAG, msg);
        }else if(LogType.ERROR.compareTo(logType) == 0){
            Log.e(TAG, msg);
        }else if(LogType.INFO.compareTo(logType) == 0){
            Log.i(TAG, msg);
        }else if(LogType.WARE.compareTo(logType) == 0){
            Log.w(TAG, msg);
        }else{
            Log.i(TAG, msg);
        }
    }

    public static void sysout(String msg){
        if(DEBUG){
            System.out.println(msg);
        }
    }

    public static void showToast(Context context, String msg) {
//    	Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        makeToast(context, msg).show();
    }

    public static void showToast(Context context, int resId) {
//    	Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
        makeToast(context, context.getResources().getString(resId)).show();
    }

    public static Toast mToast;

    private static Toast makeToast(Context context, String txt) {
        if(mToast == null)
            mToast = Toast.makeText(context.getApplicationContext(), txt, Toast.LENGTH_SHORT);
        else
            mToast.setText(txt);
        return mToast;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        if(context == null)
            return (int) dpValue;
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    /**
     * 保证只要有数就不会是0%
     * @param total
     * @param num
     * @return
     */
    public static int calPercentInt(int total, int num)
    {
        if (total == 0 || num == 0)
        {
            return 0;
        }
//		int percent = (int)((float)num / total * 100 + 0.5f);
        int percent = Math.round(num *100f/total);
//		int percent = (int) (num *100f/total);
        if(num < total) {
            return percent == 0 ? 1 : (percent < 100 ? percent : 99);
        } else {
            return percent;
        }
    }
    /**
     * 保证只要有数就不会是0%
     * @param total
     * @param num
     * @return
     */
    public static float calPercentFloat(int total, int num)
    {
        if (total == 0 || num == 0)
        {
            return 0;
        }
        float percent = (float)num / total;
        if(num < total) {
            return percent < 0.01f ? 0.01f : (percent < 0.99f ? percent : 0.99f);
        } else {
            return percent;
        }
    }
    public static int getAbsoluteScreenHeight(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(metric);
        return metric.heightPixels;
    }
    public static int getAbsoluteScreenWidth(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(metric);
        return metric.widthPixels;
    }
}
